package org.example.campsys.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Result;
import org.example.campsys.model.*;

import java.util.List;

@Mapper
public interface AdminMapper {
      @Select(" select count(id) from t_manager_student where status = #{status}")
      Integer countStudent(Integer status);

      @Select("select count(id) from t_course where status = #{status}")
      Integer countCourse(Integer status);

      @Select("select count(id) from t_manager_activity where status = #{status}")
      Integer countActivitiy(Integer status);

      @Insert("insert into t_manager_activity (name, category, title, creator, abstract, detailed_desc, status, created_time, archived_time, pic_urls, start_time, end_time)" +
              "values (#{name}, #{category}, #{title}, #{creator}, #{abs}, #{detailedDesc}, 0, #{createdTime}, #{archivedTime}, #{picUrls}, #{startTime}, #{endTime})")

      Integer addActivity(ActivityInfo activityInfo);

      @Insert("insert into t_manager_class (name, created_time, class_desc, teacher_id, activity_id) values " +
              "(#{name}, #{createdTime}, #{classDesc}, #{teacherId}, #{activityId})")
      Integer addClass(ClassInfo classInfo);


      @Select("<script> " +
              "select id, name, category, title, creator, abstract, detailed_desc, status, created_time, archived_time," +
              "pic_urls, start_time, end_time from t_manager_activity " +
              "<where> " +
              "<if test = 'name != null'> and name = #{name} </if> " +
              "<if test = 'category != null'> and category = #{category} </if> " +
              "<if test = 'title != null'> and title = #{title} </if>" +
              "<if test = 'creator != null'> and creator = #{creator} </if>" +
              "<if test = 'abs != null'> and abstract = #{abs} </if>" +
              "<if test = 'detailedDesc != null'> and detailed_desc = #{detailedDesc} </if>" +
              "<if test = 'status != null'> and status = #{status} </if>" +
              "<if test = 'createdTime != null'> and created_time = #{createdTime} </if>" +
              "<if test = 'archivedTime != null'> and archived_time = #{archivedTime} </if>" +
              "<if test = 'picUrls != null'> and pic_urls = #{picUrls} </if>" +
              "<if test = 'startTime != null'> and start_time = #{startTime} </if>" +
              "<if test = 'endTime != null'> and end_time = #{endTime} </if>" +
              "</where>" +
              "</script>")
      @Result(property = "abs", column = "abstract")
      List<ActivityInfo> selectActivity(ActivityInfo activityInfo);

      @Delete("delete from t_manager_activity where id = #{id}")
      Integer deleteActivity(Integer id);

      @Update("<script>" +
              "update t_manager_activity <set>" +
              "  <if test = 'name != null'> name = #{name}, </if>" +
              "  <if test = 'category != null'> category = #{category}, </if>" +
              "  <if test = 'title != null'> title = #{title}, </if>" +
              "  <if test = 'creator != null'> creator = #{creator}, </if>" +
              "  <if test = 'abs != null'> abstract = #{abs}, </if>" +
              "  <if test = 'detailedDesc != null'> detailed_desc = #{detailedDesc}, </if>" +
              "  <if test = 'status != null'> status = #{status}, </if>" +
              "  <if test = 'createdTime != null'> created_time = #{createdTime}, </if>" +
              "  <if test = 'archivedTime != null'> archived_time = #{archivedTime}, </if>" +
              "  <if test = 'picUrls != null'> pic_urls = #{picUrls}, </if>" +
              "  <if test = 'startTime != null'> start_time = #{startTime}, </if> " +
              "  <if test = 'endTime != null'> end_time = #{endTime} </if> </set> where id = #{id} " +
              "</script>")
      Integer updateActivity(ActivityInfo activityInfo);

      @Update("update t_manager_activity set status = #{status} where id = #{id}")
      Integer orcActivity(ActivityInfo activityInfo);

      @Select("select id, name, created_time, class_desc, teacher_id, activity_id from t_manager_class")
      List<ClassInfo> selectClass();

      @Update("update t_manager_class set activity_id = #{activityId} where id = #{id}")
      Integer cfgActivity(@Param("activityId") Integer activityId,@Param("id") Integer id);

      @Update("update t_manager_class set teacher_id = #{teacherId} where id = #{id}")
      Integer cfgTeacher(@Param("teacherId") Integer teacherId, @Param("id") Integer id);

      @Select("select t_manager_student.id, t_manager_student.stu_name, t_manager_student.from_place, t_manager_student.parent_phone, t_manager_student.current_grade, " +
              "t_manager_student.is_first, t_manager_student.login_id, t_manager_student.comment, t_manager_student.status " +
              "from t_manager_student inner join t_teaching_student_in_class on t_manager_student.id = t_teaching_student_in_class.student_id " +
              "where t_teaching_student_in_class.class_id = #{classId}")
      List<StudentInfo> selClassStu(Integer classId);

      @Update("<script>" +
              "update t_manager_class <set>" +
              "  <if test = 'name != null'>name = #{name}, </if>" +
              "  <if test = 'createdTime != null'> created_time = #{createdTime}, </if>" +
              "  <if test = 'classDesc != null'> class_desc = #{classDesc} </if> </set> where id = #{id} " +
              "</script>")
      Integer updClass(ClassInfo classInfo);

      @Delete("delete from t_manager_class where id = #{id}")
      Integer deleteClass(Integer id);

      @Select("<script> select id, stu_name, from_place, parent_phone, current_grade, is_first, login_id, comment, status from t_manager_student " +
              "<where> <if test = 'stuName != null'> and stu_name = #{stuName} </if>" +
              " <if test = 'currentGrade != null'> and current_grade = #{currentGrade} </if> </where> </script>")
      List<StudentInfo> selectStudent(StudentInfo studentInfo);

      @Insert("insert into t_teaching_student_in_class (class_id, student_id) values (#{classId}, #{studentId})")
      Integer cfgStudent(@Param("classId") Integer classId,@Param("studentId") Integer studentId);

      @Update("<script>" +
              "update t_manager_student <set> " +
              "  <if test = 'stuName != null'>  stu_name = #{stuName}, </if>" +
              "  <if test = 'fromPlace != null'>  from_place = #{fromPlace}, </if>" +
              "  <if test = 'parentPhone != null'>  parent_phone  = #{parentPhone} ,</if> " +
              "  <if test = 'currentGrade != null'>  current_grade  = #{currentGrade}, </if>" +
              "  <if test = 'isFirst != null'>  is_first = #{isFirst}, </if>" +
              "  <if test = 'loginId != null'>  login_id = #{loginId}, </if>" +
              "  <if test = 'comment != null'>  comment = #{comment}, </if>" +
              "  <if test = 'status != null'>  status = #{status} </if> </set> where id = #{id} " +
              "</script> ")
      Integer updateStudent(StudentInfo studentInfo);

      @Delete("delete from t_manager_student where id =#{id}")
      Integer deleteStudent(Integer id);

      @Insert("insert into t_manager_student (stu_name, from_place, parent_phone, current_grade, is_first, login_id, comment, status) " +
              "values ( #{stuName}, #{fromPlace}, #{parentPhone}, #{currentGrade}, #{isFirst}, #{loginId}, #{comment}, 0 )")
      Integer addStudent(StudentInfo studentInfo);

      @Update("update t_manager_student set status = #{status} where id = #{id}")
      Integer auditStudent (StudentInfo studentInfo);

      @Insert("insert into t_manager_teacher (teacher_name, subject, profession, school, introduce, login_id)" +
              "values (#{teacherName}, #{subject}, #{profession}, #{school}, #{introduce}, #{loginId})")
      Integer addTeacher(TeacherInfo teacherInfo);

      @Update("<script>" +
              "update t_manager_teacher <set>" +
              "<if test = 'teacherName != null'> teacher_name = #{teacherName}, </if>" +
              "<if test = 'subject != null'> subject = #{subject}, </if>" +
              "<if test = 'profession != null'> profession = #{profession}, </if>" +
              "<if test = 'school != null'> school = #{school}, </if>" +
              "<if test = 'introduce != null'> introduce = #{introduce}, </if>" +
              "<if test = 'loginId != null'> login_id = #{loginId} </if> </set> where id = #{id} " +
              "</script>")
      Integer updTeacher(TeacherInfo teacherInfo);

      @Select("<script> select id, teacher_name, subject, profession, school, introduce, login_id from t_manager_teacher" +
              "<where> <if test = 'teacherName != null'> and teacher_name = #{teacherName} </if>" +
              "<if test = 'subject != null'> and subject = #{subject} </if>" +
              "<if test = 'profession != null'> and profession = #{profession} </if>" +
              "<if test = 'school != null'> and school = #{school} </if>" +
              "<if test = 'introduce != null'> and introduce = #{introduce} </if>" +
              "<if test = 'loginId != null'> and login_id = #{loginId} </if> </where>" +
              "</script>")
      List<TeacherInfo> selectTeacher(TeacherInfo teacherInfo);

      @Delete("delete from t_manager_teacher where id = #{id}")
      Integer delTeacher(Integer id);
}
